<?php

class Home extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Model_user');
    }

    function index() {
		$this->login();
    }
	
	function login($error=0){
		if ($this->session->userdata('adminin') == TRUE && ($this->session->userdata('role') == 'admin' || $this->session->userdata('role') == 'superadmin')) {
            redirect(base_url() . 'admin/ctrl_user');
        }
		
		if($error==1){
			$display['is_error']=true;
		}else{
			$display['is_error']=false;
		}
		
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/login',$display);
		$this->load->view('admin/admin_footer_view');
		
	}

    function proses_login() {
        if ($role=$this->Model_user->login($this->input->post('username'), $this->input->post('password'))) {
            $data = array(
                'adminin' => TRUE,
                'username' => $this->input->post('username'),
                'role' => $role
            );
            $this->session->set_userdata($data);
            redirect(base_url() . 'admin/ctrl_user');
        }
        else{
            redirect(base_url() . 'admin/home/login/1');
        }
    }

    function logout() {
        $newdata = array('adminin' => FALSE, 'username' => '', 'role' => '');
        $this->session->unset_userdata($newdata);
        redirect(base_url() . 'admin');
    }

}

?>
